#pragma once
#include "Log.hpp"
#include "Error.hpp"
#include <sys/epoll.h>
#include <unistd.h>

static const int epollsize = 1024;
// 要关心的文件描述符的最大个数

class Epoller
{
public:
    Epoller()
    {
    }
    ~Epoller()
    {
    }
    void Create()
    {
        // int epoll_create(int size);
        _epollfd = epoll_create(epollsize);
        if (_epollfd < 0)
        {
            logMessage(Fatal, "EpollCreate failed...");
            exit(EPOLL_CREATE_ERR);
        }
    }
    void DelEvent(int fd)
    {
        int n = epoll_ctl(_epollfd,EPOLL_CTL_DEL,fd, nullptr);
        if(n == -1)
        {
            logMessage(Fatal,"DelEvent failed...");
            exit(EPOLL_DEL_ERR);
        }
    }

    void AddModEvent(int op, int fd, uint32_t events)
    {
        struct epoll_event ev;
        ev.data.fd = fd;
        ev.events = events;
        int n = epoll_ctl(_epollfd, op, fd, &ev);
        if (n == -1)
        {
            logMessage(Fatal, "AddModEvent failed...");
            exit(EPOLL_ADD_MOD_ERR);
        }
    }
    int Wait(struct epoll_event *evs, int maxnum, int timeout)
    {
        int n = epoll_wait(_epollfd, evs, maxnum, timeout);
        return n;
    }
    int Fd()
    {
        return _epollfd;
    }
    void Close()
    {
        close(_epollfd);
    }

private:
    int _epollfd;
};